#include "stdafx.h"
#include "test_instance.h"


test_instance::test_instance()
{
}


test_instance::~test_instance()
{
}

void test_instance::initialize()
{
	m_jet1.init_start_instance();
}

void test_instance::prepare()
{
	std::cout << "====================================" << std::endl
		<< "\t Instance #1" << std::endl
		<< "====================================" << std::endl;
	JET_INSTANCE_INFO_A *info;
	m_jet1.info(info);
	std::cout << " (INFO)Instance ID: " << info->hInstanceId << std::endl;
	std::cout << " (INFO)Display Name: " << info->szDatabaseDisplayName << std::endl;
	std::cout << " (INFO)Database File Name: " << info->szDatabaseFileName << std::endl;
	std::cout << " (INFO)Instance Name: " << info->szInstanceName << std::endl;
	std::cout << "------------------------------------" << std::endl;
	std::cout << " ID: " << m_jet1.id() << std::endl;
	std::cout << " Is circular log: " << m_jet1.circular_log() << std::endl;
	std::cout << " Last Error: " << m_jet1.last_error() << std::endl;
	std::cout << " Log Path: " << m_jet1.log_path() << std::endl;
	std::cout << " Maximum Log Size: " << m_jet1.log_size() << std::endl;
	std::cout << " Maximum Sessions: " << m_jet1.max_sessions() << std::endl;
	std::cout << " Maximum transaction pages: " << m_jet1.max_transaction_pages() << std::endl;
	std::cout << " System Path: " << m_jet1.system_path() << std::endl;
	std::cout << "====================================" << std::endl << std::endl;

	if (!m_jet2.initialize("TestInstance2", "TestInstance Instance 2"))
	{
		throw std::exception(ese::error(m_jet2.last_error()).c_str());
	}

	// each instance must have its own system path and log path
	path p2(m_jet1.system_path() + "2");
	if (!exists(p2))
	{
		create_directories(p2);
	}
	if (!m_jet2.system_path(p2))
	{
		throw std::exception(ese::error(m_jet2.last_error()).c_str());
	}

	if (!exists(p2 /= "Logs"))
	{
		create_directories(p2);
	}
	if (!m_jet2.log_path(p2))
	{
		throw std::exception(ese::error(m_jet2.last_error()).c_str());
	}

	std::cout << "====================================" << std::endl
		<< "\t Instance #2" << std::endl
		<< "====================================" << std::endl;
	info;
	m_jet2.info(info);
	std::cout << " (INFO)Instance ID: " << info->hInstanceId << std::endl;
	std::cout << " (INFO)Display Name: " << info->szDatabaseDisplayName << std::endl;
	std::cout << " (INFO)Database File Name: " << info->szDatabaseFileName << std::endl;
	std::cout << " (INFO)Instance Name: " << info->szInstanceName << std::endl;
	std::cout << "------------------------------------" << std::endl;
	std::cout << " ID: " << m_jet2.id() << std::endl;
	std::cout << " Is circular log: " << m_jet2.circular_log() << std::endl;
	std::cout << " Last Error: " << m_jet2.last_error() << std::endl;
	std::cout << " Log Path: " << m_jet2.log_path() << std::endl;
	std::cout << " Maximum Log Size: " << m_jet2.log_size() << std::endl;
	std::cout << " Maximum Sessions: " << m_jet2.max_sessions() << std::endl;
	std::cout << " Maximum transaction pages: " << m_jet2.max_transaction_pages() << std::endl;
	std::cout << " System Path: " << m_jet2.system_path() << std::endl;
	std::cout << "====================================" << std::endl << std::endl;
}

void test_instance::run()
{
	if (!m_jet2.start())
	{
		throw std::exception(ese::error(m_jet2.last_error()).c_str());
	}
}

void test_instance::cleanup()
{
	if (!m_jet2.stop())
	{
		throw std::exception(ese::error(m_jet2.last_error()).c_str());
	}
	if (!m_jet1.stop())
	{
		throw std::exception(ese::error(m_jet1.last_error()).c_str());
	}
}
